package com.example.knowledgeget1.KnowtController;

import com.example.knowledgeget1.Common.HttpStatusEnum;
import com.example.knowledgeget1.Common.ResponseResult;
import com.example.knowledgeget1.KnowtCommon.Entity.Student;
import com.example.knowledgeget1.KnowtCommon.Request.StudentLoginRequest;
import com.example.knowledgeget1.KnowtCommon.Response.StudentLoginResponse;
import com.example.knowledgeget1.KnowtCommon.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @PostMapping("/")
    public Student createStudent(@RequestBody Student student) {
        return studentService.createStudent(student);
    }

    @GetMapping("/")
    public List<Student> getAllStudents() {
        return studentService.getAllStudents();
    }

    @GetMapping("/{id}")
    public Student getStudentById(@PathVariable Long id) {
        return studentService.getStudentById(id);
    }

    @PutMapping("/{id}")
    public Student updateStudent(@PathVariable Long id, @RequestBody Student student) {
        return studentService.updateStudent(id, student);
    }

    @DeleteMapping("/{id}")
    public void deleteStudent(@PathVariable Long id) {
        studentService.deleteStudent(id);
    }

    // 验证学生登录并返回包含学号和token的JSON响应
    @PostMapping("/validate")
    public ResponseResult validateStudentAndGetToken(@RequestBody StudentLoginRequest loginRequest) {
        Map<String, String> response = studentService.validateStudentAndGetToken(loginRequest.getStuId(), loginRequest.getStuPwd());
        if (response != null) {
            return ResponseResult.success(response); // 验证成功，返回包含账号和token的JSON响应
        }
        return ResponseResult.fail(HttpStatusEnum.UNAUTHORIZED); // 验证失败
    }
}